package fr.oz.mobile.module.profile.day 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fr.digitas.flowearth.conf.Conf;
	import fr.oz.core.datas.ProfilListDatas;
	import fr.oz.mobile.common.ui.SelectButton;
	import fr.oz.mobile.module.IView;
	import fr.oz.mobile.ui.profile.PlaylistButton;
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class MusicSelector extends Sprite implements IView
	{
		private var setButton:SelectButton;
		private var inite:Boolean = true;
		private var profil:ProfilListDatas;
		private var container:Sprite;
		private var listTypeMusic:Array = ["playlists", "podcasts", "web radios", "alarms"]
		private var listMusic:Array = [ new Array("Classical", "Irish folk", "US rap", "movie Songs"), 
										new Array("podcasts 1", "podcasts 2", "podcasts 3"),
										new Array("web radios 1", "web radios 2", "web radios 3"),
										new Array("alarms 1", "alarms 2", "alarms 3"),
										]
		private var panel:Sprite;
		private var same:Boolean;
		private var isPanelOpen:Boolean;
		private var oldRubSelected:PlaylistButton;
		private var oldSubRubSelected:PlaylistButton;
		
		//Data a recuper du XML
		private var rubSelected:int = 2;
		private var SubRubSelected:int = 2;
		
		public function MusicSelector(_profil:ProfilListDatas) 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
			
			profil = _profil;
			
			setButton = new SelectButton();
			addChild(setButton);
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			setButton.y = stage.stageHeight - 90;
			setButton.x = (stage.stageWidth - setButton.width) >> 1;
			setButton.addEventListener(MouseEvent.CLICK, handleClick);
			
			container = new Sprite();
			addChild(container);
			
			container.addEventListener(MouseEvent.CLICK, handleClickContainer);
			
			initGUI();
		}
		
		private function initGUI():void 
		{
			for (var i:int = 0; i < listTypeMusic.length; i++ )
			{
				var playlist:PlaylistButton = new PlaylistButton(listTypeMusic[i], i);
				playlist.y = (80 * i)  + 250;
				playlist.type = "rub";
				container.addChild(playlist);
				if (i == rubSelected)
				{
					oldRubSelected = playlist;
					playlist.select();
					
				}
			}
		}
		
		private function handleClickContainer(e:MouseEvent):void 
		{
			if(e.target is PlaylistButton)
			{
				same = false;
				if ((e.target as PlaylistButton).type == "rub" && (e.target as PlaylistButton).getIsSelected() == true)
				{
					same = true;
				}
				if ((e.target as PlaylistButton).type == "rub" && (e.target as PlaylistButton).getIsSelected() == false)
				{
					inite = false;
					(e.target as PlaylistButton).select(); 
					if (oldRubSelected) oldRubSelected.unselect();
					oldRubSelected = (e.target as PlaylistButton)
					rubSelected  = (e.target as PlaylistButton).getId();
			
				}
				if ((e.target as PlaylistButton).type == "subrub")
				{
					SubRubSelected = (e.target as PlaylistButton).getId(); 
				}
				
				if (!isPanelOpen) openPanel((e.target as PlaylistButton).getId(), (e.target as PlaylistButton).getIsSelected() );
				else removePanel();
			}
		}
		
		private function handleClick(e:MouseEvent):void 
		{
			
			trace("handleCLCIK SET BUTTON");
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function openPanel(id:int, isSelected:Boolean):void 
		{
			panel = new Sprite();
			panel.graphics.beginFill(0x000000, 0.9);
			panel.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			panel.graphics.endFill();
			container.addChild(panel);
			
			isPanelOpen = true;
			
			for (var i:int = 0; i < listMusic[id].length; i++ )
			{
				var playlist:PlaylistButton = new PlaylistButton(listMusic[id][i], i);
				playlist.y = (80 * i)  + 250;
				playlist.type = "subrub";
				panel.addChild(playlist);
				if (i == SubRubSelected && inite) playlist.select();
				if( i == SubRubSelected && same) playlist.select();
			}
		}
		
		private function removePanel():void 
		{
			if (panel)
			{
				container.removeChild(panel);
				//panel = null;
				isPanelOpen = false;
			}
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
		}
		
		/* INTERFACE fr.oz.mobile.module.IView */
		
		public function getValue():ProfilListDatas
		{
			return profil;
		}
		
		public function removeView():void
		{
			
		}
		
		public function activeView():void
		{
			
		}
		
		public function get title():XMLList
		{
			return Conf.getDatas("wakeUpProfile").data.ui.wakeUpMusic;
		}
		
		public function get cachePosY():int
		{
			return 120;
		}
		
	}

}